package colibri.lcdui;

import colibri.App;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Bayron Thiengo Quinelato
 */
public class StringItem extends Item {

    private String strTitle;
    private String strText;
    private boolean hasBorder;
    private boolean hasOpen;
    private int colorTitle;
    private int colorText;
    private int colorBorder;
    private int colorFocusTitle;
    private int colorFocusText;
    private int colorFocusBorder;
    private Font fontTitle;
    private Font fontText;
    private int textX;
    private int textW;
    private int originalH;

    public StringItem(String strTitle, String strText, int x, int y, int maxWidth, int maxHeight, int anchor) {
        super(x, y, maxWidth, maxHeight, anchor);
        this.strTitle = strTitle;
        this.strText = strText;

        hasBorder = false;
        hasOpen = true;

        colorTitle = 0x666666;
        colorText = 0x666666;
        colorBorder = 0x666666;
        colorFocusTitle = 0x000000;
        colorFocusText = 0x000000;
        colorFocusBorder = 0x000000;
        fontTitle = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
        fontText = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);

        textX = x;
        textW = maxWidth;
        originalH = maxHeight;
    }

    public void setColorTitle(int colorTitle, int colorFocusTitle) {
        if (colorTitle >= 0)
            this.colorTitle = colorTitle;
        if (colorFocusTitle >= 0)
            this.colorFocusTitle = colorFocusTitle;
    }

    public void setColorText(int colorText, int colorFocusText) {
        if (colorText >= 0)
            this.colorText = colorText;
        if (colorFocusText >= 0)
            this.colorFocusText = colorFocusText;
    }

    public void setColorBorder(int colorBorder, int colorFocusBorder) {
        if (colorBorder >= 0)
            this.colorBorder = colorBorder;
        if (colorFocusBorder >= 0)
            this.colorFocusBorder = colorFocusBorder;
    }

    public void setBorder(boolean hasBorder) {
        this.hasBorder = hasBorder;
        if (hasBorder) {
            textX = getX() + 2;
            textW = getWidth() - 4;
        }
    }

    public void setOpen(boolean hasOpen) {
        this.hasOpen = hasOpen;
        if (hasOpen)
            setHeight(originalH);
        else
            setHeight(fontTitle.getHeight());
    }

    public void setFont(Font fontTitle, Font fontText) {
        if (fontTitle != null)
            this.fontTitle = fontTitle;
        if (fontText != null)
            this.fontText = fontText;
    }

    protected void callPaint(Graphics g) {
        int tmpY = getY();
        String tmpTitle = strTitle;
        // Escreve o título
        if (tmpTitle != null) {
            g.setFont(fontTitle);
            if (focus)
                g.setColor(colorFocusTitle);
            else
                g.setColor(colorTitle);
            if (hasOpen)
                tmpTitle += " -";
            else
                tmpTitle += " +";
            if (fontTitle.stringWidth(tmpTitle) < textW && tmpTitle.indexOf("\n") < 0) {
                g.drawString(tmpTitle, textX, tmpY, anchor);
                tmpY += fontTitle.getHeight();
            } else {
                int indexSpace = -1;
                int indexStarLine = 0;
                for (int j = 0; j < tmpTitle.length(); j++) {
                    if (tmpTitle.charAt(j) == '\n') {
                        indexSpace = j;
                        if (indexSpace < 0) {
                            indexSpace = j - 1;
                        }
                        g.drawSubstring(tmpTitle, indexStarLine, indexSpace - indexStarLine, textX, tmpY, Graphics.TOP | Graphics.LEFT);
                        indexStarLine = indexSpace;
                        indexSpace = -1;
                        tmpY += fontTitle.getHeight();
                    } else {
                        if (fontTitle.stringWidth(tmpTitle.substring(indexStarLine, j)) > textW) {
                            if (indexSpace < 0) {
                                indexSpace = j - 1;
                            }
                            g.drawSubstring(tmpTitle, indexStarLine, indexSpace - indexStarLine, textX, tmpY, Graphics.TOP | Graphics.LEFT);
                            indexStarLine = indexSpace;
                            indexSpace = -1;
                            tmpY += fontTitle.getHeight();
                        } else {
                            if (j == tmpTitle.length() - 1) {
                                g.drawSubstring(tmpTitle, indexStarLine, tmpTitle.length() - indexStarLine, textX, tmpY, Graphics.TOP | Graphics.LEFT);
                                tmpY += fontTitle.getHeight();
                            } else if (tmpTitle.charAt(j) == ' ') {
                                indexSpace = j + 1;
                            }
                        }
                    }
                }
            }
        }
        // Escreve o texto
        if (strText != null && hasOpen) {
            g.setFont(fontText);
            if (focus)
                g.setColor(colorFocusText);
            else
                g.setColor(colorText);
            if (fontText.stringWidth(strText) < textW && strText.indexOf("\n") < 0) {
                g.drawString(strText, textX, tmpY, anchor);
                tmpY += fontText.getHeight();
            } else {
                int indexSpace = -1;
                int indexStarLine = 0;
                for (int j = 0; j < strText.length(); j++) {
                    if (strText.charAt(j) == '\n') {
                        indexSpace = j;
                        if (indexSpace < 0) {
                            indexSpace = j - 1;
                        }
                        g.drawSubstring(strText, indexStarLine, indexSpace - indexStarLine, textX, tmpY, Graphics.TOP | Graphics.LEFT);
                        indexStarLine = indexSpace;
                        indexSpace = -1;
                        tmpY += fontText.getHeight();
                    } else {
                        if (fontText.stringWidth(strText.substring(indexStarLine, j)) > textW) {
                            if (indexSpace < 0) {
                                indexSpace = j - 1;
                            }
                            g.drawSubstring(strText, indexStarLine, indexSpace - indexStarLine, textX, tmpY, Graphics.TOP | Graphics.LEFT);
                            indexStarLine = indexSpace;
                            indexSpace = -1;
                            tmpY += fontText.getHeight();
                        } else {
                            if (j == strText.length() - 1) {
                                g.drawSubstring(strText, indexStarLine, strText.length() - indexStarLine, textX, tmpY, Graphics.TOP | Graphics.LEFT);
                                tmpY += fontText.getHeight();
                            } else if (strText.charAt(j) == ' ') {
                                indexSpace = j + 1;
                            }
                        }
                    }
                }
            }
        }
        // Desenha a borda
        if (hasBorder) {
            if (focus)
                g.setColor(colorFocusBorder);
            else
                g.setColor(colorBorder);
            if (hasOpen) {
                if (tmpTitle == null) {
                    // Borda esquerda
                    g.drawLine(getX(), getY(), getX(), getY() + getHeight() - 1);
                    // Borda direita
                    g.drawLine(getX() + getWidth() - 1, getY(), getX() + getWidth() - 1, getY() + getHeight() - 1);
                    // Borda Superior
                    g.drawLine(getX(), getY(), getX() + getWidth() - 1, getY());
                } else {
                    // Borda esquerda
                    g.drawLine(getX(), getY() + fontTitle.getHeight() / 2, getX(), getY() + getHeight() - 1);
                    // Borda direita
                    g.drawLine(getX() + getWidth() - 1, getY() + fontTitle.getHeight() / 2, getX() + getWidth() - 1, getY() + getHeight() - 1);
                    // Borda Superior
                    if (fontTitle.stringWidth(tmpTitle) < getWidth())
                        g.drawLine(getX() + textX + fontTitle.stringWidth(tmpTitle), getY() + fontTitle.getHeight() / 2, getX() + getWidth() - 1, getY() + fontTitle.getHeight() / 2);
                }
                // Borda Inferior
                g.drawLine(getX(), getY() + getHeight() - 1, getX() + getWidth() - 1, getY() + getHeight() - 1);
            } else {
                // Borda Superior
                if (tmpTitle == null)
                    g.drawLine(getX(), getY(), getX() + getWidth() - 1, getY());
                else if (fontTitle.stringWidth(tmpTitle) < getWidth())
                    g.drawLine(getX() + textX + fontTitle.stringWidth(tmpTitle), getY() + fontTitle.getHeight() / 2, getX() + getWidth() - 1, getY() + fontTitle.getHeight() / 2);
            }
        }
    }

    protected void callKeyReleased(int keyCode) {
        int gameAction = App.getGameAction(keyCode);
        switch (gameAction) {
            case App.FIRE:
                if (focus && hasOpen)
                    hasOpen = false;
                else
                    hasOpen = true;
                break;
        }
    }

}
